import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcInsert2 {
    public static void main(String[] args) throws SQLException {
        //1.创建并初始化数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/3_15?characterEncoding=utf8&useSSL= false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");
        //2.建立连接;
        Connection connection = dataSource.getConnection();
        //3.构造sql语句
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4.编译sql语句
        ResultSet resultSet = statement.executeQuery();
        //5.遍历结果集合
        while (resultSet.next()){
            int id = resultSet.getInt("ID");
            String name = resultSet.getNString("name");
            System.out.println("id = "+id+" name = "+name+"");
        }
        //6.释放资源
    }
}
